//
//  ViewController.m
//  17--WeekendHomeWork
//
//  Created by 冀永金 on 16/8/5.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
#import "ContactTableViewCell.h"
#import "Contact.h"
#import "VCOfSecond.h"
#import "VCOfThird.h"
#import "ReadandSaveCaches.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,VCOfThirdDelegate,VCOfDelegate>
@property (nonatomic, strong) UITableView *tableViewOfContact;
@property (nonatomic, strong) NSMutableArray *marrOfContact;
@property (nonatomic, strong) NSMutableArray *contact;
@property (nonatomic, strong)  ContactTableViewCell *cell;
@property (nonatomic, assign) NSInteger count;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"通讯录";
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    [self createTableView];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"DLS160710" ofType:@"plist"];
        self.marrOfContact = [NSArray arrayWithContentsOfFile:path].mutableCopy;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(handleContact:)];
   
  
    if ([ReadandSaveCaches readAnyObjectFromCaches:@"caches.plist"]) {
        self.contact = [NSMutableArray array];
       id anyObject = [ReadandSaveCaches readAnyObjectFromCaches:@"caches.plist"];
        for (NSDictionary *dic in anyObject) {
            Contact *contact = [[Contact alloc]init];
            [contact setValuesForKeysWithDictionary:dic];
            [self.contact addObject:contact];
        }
       
    } else {
         self.contact = [NSMutableArray array];
        [ReadandSaveCaches  saveAnyObjectIntoCaches:self.marrOfContact FileName:@"caches.plist"];
    for (NSDictionary *dic in self.marrOfContact) {
        Contact *contact = [[Contact alloc]init];
        [contact setValuesForKeysWithDictionary:dic];
        [self.contact addObject:contact];
    }
       
}
   // [self.tableViewOfContact reloadData];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)createTableView {
    self.tableViewOfContact = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableViewOfContact];
    self.tableViewOfContact.delegate = self;
    self.tableViewOfContact.dataSource = self;
    [_tableViewOfContact registerClass:[ContactTableViewCell class] forCellReuseIdentifier:@"pool"];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableViewOfContact setEditing:editing animated:editing];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
       
    [self.marrOfContact removeObjectAtIndex:indexPath.row];
    [self.contact  removeObjectAtIndex:indexPath.row];
    [ReadandSaveCaches  saveAnyObjectIntoCaches:self.marrOfContact FileName:@"caches.plist"];
//   self.marrOfContact =  [ReadandSaveCaches readAnyObjectFromCaches:@"caches.plist"];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    
    //[self.tableViewOfContact reloadData];
}




- (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contact.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   _cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    Contact *contact = self.contact[indexPath.row];
    [_cell setValuesWithContact:contact];
    return _cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    VCOfSecond *second = [[VCOfSecond alloc]init];
    [self.navigationController pushViewController:second animated:YES];
    second.delegate = self;
    Contact *contact = self.contact[indexPath.row];
    second.name = contact.name;
    second.genderContent = contact.gender;
    second.phoneContent = contact.phoneNumber;
    
   
}
- (void)handleContact:(UIBarButtonItem *)item {
    VCOfThird *third = [[VCOfThird alloc]init];
    [self.navigationController pushViewController:third animated:YES];
    third.delegate = self;
}

- (void)addContact:(NSMutableDictionary *)dic {
    //self.marrOfContact = [NSMutableArray array];
    [self.marrOfContact addObject:dic];
    Contact *contact = [[Contact alloc]init];
    [contact setValuesForKeysWithDictionary:dic];
    [self.contact addObject:contact];
    
    [ReadandSaveCaches saveAnyObjectIntoCaches:self.marrOfContact  FileName:@"caches.plist"];

    [self.tableViewOfContact reloadData];
    NSLog(@"%@",NSHomeDirectory());
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
   self.marrOfContact =  [ReadandSaveCaches readAnyObjectFromCaches:@"caches.plist"];
//    for (NSDictionary *dic in self.marrOfContact) {
//        Contact *contact = [[Contact alloc]init];
//        [contact setValuesForKeysWithDictionary:dic];
//        [self.contact addObject:contact];
//    }

   // [self.tableViewOfContact reloadData];
}
- (void)getImage:(VCOfSecond *)vc {
    _cell.image.image = vc.image.image;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
